<?php
!defined('M_P') && exit('Forbidden');

/**
 * 经纪人相关的会话bean
 */
class PW_BrokerModule {
	var $uid;//用户id
	var $username;//用户名
	var $groupid;//经纪人组id
	var $realname;//真实姓名
	var $ifactive;//是否激活
	var $area;//主营区域
	var $plate;//主营版块
	var $company;//工作公司
	var $address;//地址
	var $email;//邮箱地址
	var $tel;//手机电话
	var $phone;//固定电话
	var $verify;//认证类型
	var $idcard;//身份证号码
	var $idcardpic;//身份证图片
	var $alipay;//支付宝帐号
	var $otherinfo;//其他信息
	var $remark;//备注信息
	var $createtimevar;//创建时间
	var $announce;//店铺公告
	var $business;//主营业务，二手房或租房
	
	var $verifyInfo;//认证类型详细信息
	
	var $_filterUitil;//词语过滤
	
	function __construct() {
		$this->_filterUitil = L::loadClass('FilterUtil', 'filter');
	}
	
	function PW_BrokerModule() {
		$this->__construct();
	}
	/**
	 * 设置经纪人的uid
	 */
	function setUid($uid) {
		$uid = (int) $uid;
		$this->uid = $uid;
	}
	/**
	 * 设置经纪人的用户名
	 * @param $username
	 */
	function setUsername($username) {
		if (!trim($username)) Error::addError('请填写用户名');
		$userService = L::loadClass('userservice','user');
		$userInfo = $userService->getByUserName($username);
		if (!$userInfo) Error::addError('该用户不存在');
		$this->username = $username;
		$this->setUid($userInfo['uid']);
	}
	/**
	 * 设置经纪人的经纪人组
	 * @param int $groupid
	 */
	function setGroupid($groupid) {
		$groupid = (int) $groupid;
		if (!$groupid) Error::addError('请选经纪人组');
		$this->groupid = $groupid;
	}
	function setRealname($realname) {
		if (!trim($realname)) Error::addError('请填写姓名');
		$this->realname = $realname;
	}
	
	function setIfactive($ifactive){
		$this->ifactive = (int)$ifactive;
	}
	
	function setArea($area) {
		$this->area = (int)$area;
	}
	function setPlate($plate) {
		$this->plate = $plate;
	}
	function setCompany($company) {
		$this->company = $company;
	}
	function setAddress($address) {
		$this->address = $address;
	}
	function setEmail($email) {
		$this->email = $email;
	}
	function setTel($tel) {
		if ($tel && !preg_match('/^[\d\-]{7,15}$/', $tel)) Error::addError('电话号码格式不正确');
		$this->tel = $tel;
	}
	function setPhone($phone) {
		//$phone = (int)$phone;
		if($phone && !preg_match('/^\d{11,15}$/', $phone)) Error::addError('手机号码格式不正确');
		$this->phone = $phone;
	}
	/**
	 * 设置认证类型
	 * @param $verify
	 */
	function setVerify($verify) {
		if (!S::isArray($verify)){
			$this->verify = 0;
			return '';
		}
		$temp = 0;
		foreach ($verify as $key=>$value) {
			if (!$value) continue;
			$temp = $temp|$key;
		}
		$this->verify = $temp;
	}
	function setIdcard($idcard) {
		$this->idcard = $idcard;
	}
	function setIdcardpic() {
		
		House::loadClass('IdcardUpload', 'upload', false);
		$idCardUpload = new IdcardUpload($this->uid);
		PwUpload::upload($idCardUpload);
		if($idCardUpload->pathname){
			$this->idcardpic = $idCardUpload->pathname;
		}
	}
	function setAlipay($alipay) {
		$this->alipay = $alipay;
	}
	function setOtherinfo($otherinfo) {
		$this->otherinfo = $otherinfo;
	}
	function setRemark($remark) {
		$this->remark = $remark;
	}
	function setCreatetime($timestamp) {
		$this->createtime = $timestamp;
	}
	
	function setAnnounce($announce){
		if (($banword = $this->_filterUitil->comprise($announce)) !== false) {
			Error::addError('警告：内容中含有非法词语 \''.$banword.'\'！');
		}
		$this->announce = nl2br($announce);		
	}

	function setBusiness($business) {
		$this->business = $business;
	}
	
	function setIfmsg($ifmsg){
		if($ifmsg){
			global $windid;
			$brokerGroupService = House::loadService('brokerGroupService');
			$brokerGroup = $brokerGroupService->getBrokerGroupRight($this->groupid);
			if($this->ifactive){
				$title = '房产经纪人账号已激活';
				$content = "您的 {$brokerGroup['name']} 已经激活。";
				if($brokerGroup['ifhire']) $content .=  $brokerGroup['hirenum'] > 0 ? "每日可以发布出租房源{$brokerGroup['hirenum']}套，" : "每日发布出租房源不限，";
				if($brokerGroup['ifsale']) $content .=  $brokerGroup['salenum'] > 0 ? "每日可以发布出售房源{$brokerGroup['salenum']}套" : "每日发布出售房源不限";
			}else{
				$title = '房产经纪人账号已被锁定';
				$content = "您的 {$brokerGroup['name']} 已经被{$windid}锁定。你不再具有相关权限。";
			}
			M::sendNotice(array($this->username), array('title' => $title, 'content' => $content));
		}	
	}

	function checkTelOrPhone(){
		if(!$this->tel && !$this->phone){
			Error::addError('固定电话与手机号码至少填写一项');
		}
	}
	
	function checkIfBroker(){
		$brokerService = House::loadService('BrokerService');
		if($brokerService->getBroker($this->uid)){
			adminmsg("该用户已经是经纪人，需要编辑请<a href={$GLOBALS['basename']}&action=edit&uid={$this->uid} style='color:red'>点击此处</a>");
		}
	}
}